Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmSAL100
		Inherits System.Windows.Forms.Form
		Dim OldMMSNO As String
		Private Sub WriteToFile()
			Dim cmdExecute As Object
			Dim SQLInsert As String
			If ISADDING Then
				SQLInsert = " INSERT INTO SMM " & "(MMSNO,MMNME) " & "VALUES " & "('" & txtMMSNO.Text & "', '" & txtMMNME.Text & "')"
			Else
				SQLInsert = "UPDATE SMM SET MMSNO = '" & txtMMSNO.Text & "', MMNME ='" & txtMMNME.Text & "' " & " WHERE MMSNO ='" & OldMMSNO & "'"

			End If

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQLInsert
			cmdExecute.Execute()

		End Sub

		Private Sub WriteToScreen()
			Dim TBL As ADODB.Recordset
			If ISADDING Then 'gan rong
				txtMMSNO.Text = ""
				txtMMNME.Text = ""
				' txtUMCVF = 0
			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select SMM.*From SMM where  MMSNO ='" & cboMMSNO.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtMMSNO.Text = IIf(IsDBNull(TBL.Fields("MMSNO").Value), "", TBL.Fields("MMSNO").Value)
					txtMMNME.Text = IIf(IsDBNull(TBL.Fields("MMNME").Value), "", TBL.Fields("MMNME").Value)
					'    txtUMCVF = IIf(IsNull(TBL!UMCVF), "", TBL!UMCVF)
				Else
					cboMMSNO.BoundText = ""
					txtMMSNO.Text = ""
					txtMMNME.Text = ""
					'   txtUMCVF = 0
				End If
				TBL.Close()
			End If
			OldMMSNO = txtMMSNO.Text
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			Frame1.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub cboMMSNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboMMSNO.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen()
			End If


		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()

			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
			txtMMSNO.Text = cboMMSNO.BoundText
			WriteToScreen()


		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim Response As Object
			Dim SQL As String
			Response = MsgBox("Do you really to delte this Sale man?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.DefaultButton2, "Chu y'")
			If Response = MsgBoxResult.Yes Then
				SQL = "DELETE FROM SMM WHERE MMSNO = '" & cboMMSNO.BoundText & "'"

				cmdExecute = New ADODB.Command
				cmdExecute.ActiveConnection = DBconn
				cmdExecute.CommandText = SQL
				cmdExecute.Execute()
				Adodc1.Refresh()
			End If
			'WriteToScreen (0)

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True


			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click


			WriteToFile()
			Adodc1.Refresh()

			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()


		End Sub

		Private Sub frmSAL100_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			'BIENCHUNG
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "SMM"	'SALEMAN
			Adodc1.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
		End Sub

		Private Sub txtMMSNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtMMSNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldMMSNO, txtMMSNO, "SMM", " MMSNO='" & txtMMSNO.Text & "'") Then
					MsgBox("This code is already exist")
					txtMMSNO.Focus()
				Else
					txtMMNME.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace